package com.hnote.hnoteapplication.ui.activity;

import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;

import com.hnote.hnoteapplication.R;
import com.hnote.hnoteapplication.base.DataBindingActivity;
import com.hnote.hnoteapplication.databinding.ActivityCalculatorBinding;
import com.hnote.hnoteapplication.ui.utils.calculator.Optioner;
import com.hnote.hnoteapplication.ui.utils.calculator.OptionerFactory;

/**
 * =====================================================
 * 功    能： 计算器
 * 作    者： 加菲猫
 * 邮    箱： pfliu@3box.cc
 * 时    间： 2017/3/9 0009 17:34
 * =====================================================
 */
public class CalculatorActivity extends DataBindingActivity<ActivityCalculatorBinding> {

    // 运算符号
    private String opt = "";
    // 第一个运算数
    private double numOne;
    // 第二个运算符
    private double numTwo;

    @Override
    public int getLayoutId() {
        return R.layout.activity_calculator;
    }

    @Override
    public void initView() {

        optOnClick();
    }

    private void optOnClick() {

        mViewBinding.num0Btn.setOnClickListener((v) -> numShow("0"));

        mViewBinding.num1Btn.setOnClickListener((v) -> numShow("1"));

        mViewBinding.num2Btn.setOnClickListener((v) -> numShow("2"));

        mViewBinding.num3Btn.setOnClickListener((v) -> numShow("3"));

        mViewBinding.num4Btn.setOnClickListener((v) -> numShow("4"));

        mViewBinding.num5Btn.setOnClickListener((v) -> numShow("5"));

        mViewBinding.num6Btn.setOnClickListener((v) -> numShow("6"));

        mViewBinding.num7Btn.setOnClickListener((v) -> numShow("7"));

        mViewBinding.num8Btn.setOnClickListener((v) -> numShow("8"));

        mViewBinding.num9Btn.setOnClickListener((v) -> numShow("9"));

        mViewBinding.addBtn.setOnClickListener((v) -> {numShow("+");opt = "+";});

        mViewBinding.subBtn.setOnClickListener((v) -> {numShow("-");opt = "-";});

        mViewBinding.mulBtn.setOnClickListener((v) -> {numShow("×");opt = "*";});

        mViewBinding.divBtn.setOnClickListener((v) -> {numShow("÷");opt = "/";});

        mViewBinding.overBtn.setOnClickListener((v) -> {numShow("%");opt = "%";});

        mViewBinding.clearBtn.setOnClickListener((v) -> {
            Snackbar.make(mViewBinding.rootLayout, "清除内容！", Snackbar.LENGTH_SHORT)
                    .setAction("确定", (view) -> {
                                // Perform anything for the action selected
                            }
                    )
                    .setActionTextColor(ContextCompat.getColor(CalculatorActivity.this, R.color.gray_696969))
                    .setDuration(1000).show();
            mViewBinding.numberTv.setText("");
            mViewBinding.resultTv.setText("");
            numOne = 0;
            numTwo = 0;
            opt = "";

        });

        mViewBinding.backspaceBtn.setOnClickListener((v) -> {});

        mViewBinding.pointBtn.setOnClickListener((v) -> numShow("."));

        mViewBinding.equalBtn.setOnClickListener((v) -> {
            Optioner optioner = OptionerFactory.createOptioner(opt);
            optioner.setNumberOne(numOne);
            optioner.setNumberTwo(numTwo);

            mViewBinding.resultTv.setText(String.valueOf(optioner.getResult()));
        });
    }

    private void numShow(String text) {
        if (!(text.equals("+") || text.equals("-") || text.equals("×") || text.equals("÷"))) {

            if (null == opt || opt.equals("")) {
                numOne = Double.parseDouble(text);
            } else {
                numTwo = Double.parseDouble(text);
            }
        }

        text = mViewBinding.numberTv.getText().toString() + text;
        mViewBinding.numberTv.setText(text);
    }
}
